package cc.autoapi.pucong.auto.flow.provider.aop.manager;


import cc.autoapi.pucong.auto.flow.core.root.utils.UUID;
import cc.autoapi.pucong.auto.flow.persistence.po.aop.ProjectAopRulePo;
import cc.autoapi.pucong.auto.flow.persistence.utils.jdbc.DatabaseUtils;
import cc.autoapi.pucong.auto.flow.provider.aop.entity.ProjectAopRuleBeanConvert;
import cc.autoapi.pucong.auto.flow.provider.aop.entity.dto.ProjectAopRuleInsertDto;
import cc.autoapi.pucong.auto.flow.provider.aop.entity.dto.ProjectAopRuleUpdateDto;
import cc.autoapi.pucong.auto.flow.provider.aop.entity.vo.ProjectAopRuleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 项目aop 关联规则信息 service实现类
 *
 * @author pucong
 * @url https://gitee.com/pucong
 * @create 2023-11-21
 */
@Service
@Slf4j
public class AopRuleManager {

    @Autowired
    private ProjectAopRuleBeanConvert projectAopRuleBeanConvert;

    public List<ProjectAopRuleVo> getProjectAopRuleList(String aopId) {
        List<Object> params = new ArrayList<>();
        params.add(aopId);
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_aop_rule where 1=1 and aop_id = ? order by create_time asc");
        List<ProjectAopRuleVo> projectAopRuleVos = DatabaseUtils.execQueryListSql(params, sqlBf.toString(), ProjectAopRuleVo.class);
        return projectAopRuleVos;
    }

    public ProjectAopRuleVo getProjectAopRuleById(String id) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_aop_rule where 1=1 and uuid = ?");
        params.add(id);
        ProjectAopRuleVo projectAopRuleVo = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), ProjectAopRuleVo.class);
        return projectAopRuleVo;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insertProjectAopRule(ProjectAopRuleInsertDto dto) {
        ProjectAopRulePo aopRulePo = ProjectAopRuleBeanConvert.INSTANCE.dto2do(dto);
        aopRulePo.setUuid(UUID.uuid());
        aopRulePo.setCreateTime(new Date());
        Integer i = DatabaseUtils.saveAuto("auto_flow_aop_rule", aopRulePo);
        return i;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateProjectAopRuleById(String id, ProjectAopRuleUpdateDto dto) {
        ProjectAopRulePo aopRulePo = ProjectAopRuleBeanConvert.INSTANCE.dto2do(dto);
        aopRulePo.setUuid(id);
        Integer i = DatabaseUtils.updateAutoById("auto_flow_aop_rule", aopRulePo);
        return i;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int batchDeleteProjectAopRuleByIds(List<String> ids) {
        if (ids == null || ids.size() < 1) {
            return 0;
        }
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("delete from auto_flow_aop_rule where uuid in (");
        for (int i = 0; i < ids.size(); i++) {
            sqlBf.append("?,");
        }
        sqlBf.deleteCharAt(sqlBf.length() - 1);
        sqlBf.append(")");
        List<Object> params = new ArrayList<>();
        params.addAll(ids);

        DatabaseUtils.execSql(params, sqlBf.toString(), Integer.class);
        return ids.size();
    }
}
